import { Component, OnInit, ViewChild } from '@angular/core';
import { SysMenuService } from './sys-menu.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { MenuEditComponent } from './edit/menu-edit.component';
import { NzModalService } from 'ng-zorro-antd/modal';

export interface TreeNodeInterface {
  menuCode: string;
  menuName: string;
  url?: number;
  level?: number;
  expand?: boolean;
  icon?: string;
  children?: TreeNodeInterface[];
  parent?: TreeNodeInterface;
}

@Component({
  selector: 'app-menu-list',
  templateUrl: './menu-list.component.html',
  styles: []
})
export class MenuListComponent implements OnInit {

  @ViewChild('editComponent', {static: false})
  public editComponent: MenuEditComponent;

  public loading = false;

  constructor(private menService: SysMenuService,
              private msgSrv: NzMessageService,
              private modal: NzModalService,
  ) {

  }

  listOfMapData: TreeNodeInterface[] = [];
  mapOfExpandedData: { [menuCode: string]: TreeNodeInterface[] } = {};

  collapse(array: TreeNodeInterface[], data: TreeNodeInterface, $event: boolean): void {
    if (!$event) {
      if (data.children) {
        data.children.forEach(d => {
          const target = array.find(a => a.menuCode === d.menuCode)!;
          target.expand = false;
          this.collapse(array, target, false);
        });
      } else {
        return;
      }
    }
  }

  convertTreeToList(root: TreeNodeInterface): TreeNodeInterface[] {
    const stack: TreeNodeInterface[] = [];
    const array: TreeNodeInterface[] = [];
    const hashMap = {};
    stack.push({...root, level: 0, expand: true});

    while (stack.length !== 0) {
      const node = stack.pop()!;
      this.visitNode(node, hashMap, array);
      if (node.children) {
        for (let i = node.children.length - 1; i >= 0; i--) {
          stack.push({...node.children[i], level: node.level! + 1, expand: true, parent: node});
        }
      }
    }

    return array;
  }

  visitNode(node: TreeNodeInterface, hashMap: { [menuCode: string]: boolean }, array: TreeNodeInterface[]): void {
    if (!hashMap[node.menuCode]) {
      hashMap[node.menuCode] = true;
      array.push(node);
    }
  }

  ngOnInit(): void {
    this.loadMenuData();
  }

  loadMenuData() {

    this.loading = true;

    this.menService.getAll().subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.listOfMapData = res.data;

        this.listOfMapData.forEach(item => {
          this.mapOfExpandedData[item.menuCode] = this.convertTreeToList(item);
        });

      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  openEdit(record ?: any): void {
    this.editComponent.edit(record);
    console.log(record);
  }

  delete(id): void {
    this.modal.confirm({
      nzTitle: '确定删除这个菜单吗?',
      nzOkText: 'Yes',
      nzOkType: 'danger',
      nzOnOk: () => {
        this.menService.delete(id).subscribe((res) => {
          if (res.code === 1) {
            this.msgSrv.success('删除完成');
            this.loadMenuData();
          } else {
            this.msgSrv.error(res.message);
          }
        });
      },
      nzCancelText: 'No',
      nzOnCancel: () => console.log('Cancel')
    });
  }


  onSmt() {
    this.loadMenuData();
  }

  _onReuseInit() {
    this.loadMenuData();
  }

}
